﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MSCRM.DataLoad
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length == 1)
                {
                    DataLoad dataload = new DataLoad();
                    dataload.Execute(args[0].ToString());
                }
                else if (args.Length == 2)
                {
                    DataLoad dataload = new DataLoad();
                    dataload.Execute(args[0].ToString(), args[1].ToString());
                }
                else
                {
                    Console.WriteLine("Wrong number of arguments!");
                    ArgsHelp();
                }
            }
            catch (Microsoft.Xrm.Sdk.InvalidPluginExecutionException ipee)
            {
                Console.WriteLine(ipee.ErrorCode.ToString());
                Console.WriteLine(ipee.Message);
                Console.WriteLine(ipee.StackTrace);
                if (ipee.InnerException != null)
                {
                    Console.WriteLine(ipee.InnerException.Message);
                    Console.WriteLine(ipee.InnerException.StackTrace);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);
                    Console.WriteLine(ex.InnerException.StackTrace);
                }
            }
            finally
            {
                Console.WriteLine("Finished!");
            }
        }

        static void ArgsHelp()
        {
            Console.WriteLine("[config.xml]    :Path to config file");
            Console.WriteLine("[n]    :Connection number (optional)");
            Console.WriteLine("e.g. >Nine.MSCRM.DataLoad.exe config.xml 1");
        }
    }
}
